package com.frame.controller;


import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import com.base.controller.BaseController;
import com.base.util.RC2;
import com.base.util.Tools;
import com.frame.service.LoginManager;
import com.frame.service.MenuManager;
import com.local.service.ReportManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.base.util.PageData;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//import com.base.service.manager.LoginManager;
//import com.base.service.manager.MenuManager;
//import com.base.service.manager.RoleManager;


/**
 * Created by lee on 2016/6/23.
 * This Controller is used for login & logout
 */
@Controller
public class LoginController extends BaseController {

    @Resource(name = "loginService")
    private LoginManager loginManager;

    @Resource(name = "menuService")
    private MenuManager menuManager;

    @Resource(name = "reportService")
    private ReportManager reportManager;

    /**
     * 登录界面
     * @return
     */
    @RequestMapping(value="/login_toLogin")
    public ModelAndView goLogin(){
        ModelAndView mv = this.getModelAndView();
//        mv.setViewName("system/main/login");
        mv.setViewName("system/main/index-login");
        return mv;
    }

    /**
     * 验证登录用户名和密码
     * 方法：用户名+“tbm”+密码 拼成串，进行MD5加密，加密之后的串和TB_USER中C_PASSWORD进行比较
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/sign_up_commit",produces="application/json;charset=UTF-8")
    public Map<String,String> login(){
        Map<String,String> map = new HashMap<String, String>();
        PageData pd = this.getPageData();
        String result = "error";
        HttpSession session = this.getRequest().getSession();

        //过渡时期，为了不影响客户端和Web用户同时访问，现做如下处理：
        // 如果用户修改了密码，则覆盖：J_PASSWORD
        // 如果用户不修改密码，则使用：C_PASSWORD

        //用户密码一
//        String realpass = MD5.md5(pd.getString("username")+"tbm"+pd.getString("password"));

        //用户密码二
//        String pass = "s2mbVqbK3jE=";
//
//        pd.put("realpass",realpass);
        String realPass = RC2.getRealPass(pd.getString("password"));
//        PageData javaUser = loginManager.getUserByRealPass(pd);

        pd.put("PASS",realPass);
        pd.put("USERNAME",pd.getString("username"));
        PageData cUser = loginManager.getUserByPassAndName(pd);
//
//        if(null!=javaUser){
//            session.setAttribute("USER",javaUser);
//            result = "success";
//        }else
        if(null != cUser){
            session.setAttribute("USER",cUser);
            result = "success";
        }
        map.put("result",result);
        return map;
    }
    /**
     * 登录成功， 获取所有角色，此系统问二级目录
     * @return
     */
    @RequestMapping(value = "/login_index")
    public ModelAndView getMainIndex(){
        ModelAndView mv = this.getModelAndView();
        HttpSession session = this.getRequest().getSession();
        PageData pd = (PageData) session.getAttribute("USER");
        pd.put("RPTYEAR", Tools.date2StrYear(new Date()));
        //获取本年度填报信息
        List<PageData> reportInfo = reportManager.getReportInfo(pd);
        int rpt_flag = 0;
        String flag = "true";
        //用来判断是否是今年提交
        if(!pd.get("C_ROLEID").toString().equals("1020")){
            flag = "false";
        }else if(reportInfo.size()!=0){
            rpt_flag = Integer.valueOf(reportInfo.get(0).get("STATE").toString());
            flag = "false";

        }
        session.setAttribute("user",pd);

        mv.addObject("rpt_flag",rpt_flag);
        mv.addObject("flag",flag);
        mv.setViewName("system/main/index-v2");
        return mv;
    }

    /**
     * LogOut
     * @return
     */
    @RequestMapping(value = "/logout")
    public ModelAndView logout(){
        ModelAndView mv = this.getModelAndView();
        //清除所有session
        HttpSession session = this.getRequest().getSession();
        session.removeAttribute("USER");
        session.removeAttribute("PD");
        session.removeAttribute("MENURESULT");
        session.removeAttribute("RIGHTS");
        session.invalidate();
        mv.setViewName("system/main/index-login");
        return mv;
    }

}
